package com.example.nonacademic_education.service.impl;

import com.example.nonacademic_education.constant.Constant;
import com.example.nonacademic_education.dao.DepartmentDao;
import com.example.nonacademic_education.dao.FiDepAssociationDao;
import com.example.nonacademic_education.dao.FileItemInfoDao;
import com.example.nonacademic_education.data.request.FileItemGrantVo;
import com.example.nonacademic_education.data.response.FileItemGrantDto;
import com.example.nonacademic_education.entity.Department;
import com.example.nonacademic_education.entity.FileItemInfo;
import com.example.nonacademic_education.service.FileItemInfoService;
import com.example.nonacademic_education.util.FileUtils;
import com.example.nonacademic_education.util.R;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FileItemInfoServiceImpl implements FileItemInfoService {
    @Resource
    private FileItemInfoDao fileItemInfoDao;
    @Resource
    private FiDepAssociationDao fiDepAssociationDao;
    @Resource
    private DepartmentDao departmentDao;
    @Resource
    private FileUtils fileUtils;

    @Override
    public R getList(Integer page, Integer size, String name) {
        if (page < 1) {
            return R.error("页数不能小于1");
        }
        page = (page - 1) * size;
        List<FileItemInfo> list = fileItemInfoDao.getList(page, size, name);
        Integer total = fileItemInfoDao.count(name);
        Map<String, Object> data = new HashMap<>();
        data.put("list", list);
        data.put("total", total);
        return R.ok(data, "success");
    }

    @Override
    public R addOne(FileItemInfo fileItemInfo) {
        fileItemInfo.setId(null);
        if (fileItemInfo.getType() == null || fileItemInfo.getType().equals(Constant.EMPTY)) {
            fileItemInfo.setType("*");
        }
        fileItemInfoDao.addOne(fileItemInfo);
        return R.ok("addOne");
    }

    @Override
    public R updateOne(FileItemInfo fileItemInfo) {
        if (fileItemInfo.getId() == null) {
            return R.error("id is null");
        }
        if (fileItemInfo.getType() == null || fileItemInfo.getType().equals(Constant.EMPTY)) {
            fileItemInfo.setType("*");
        }
        fileItemInfoDao.updateOneById(fileItemInfo);
        return R.ok("updateOne");
    }

    @Override
    public R deleteOne(Long id) {
        FileItemInfo fileItemInfo = fileItemInfoDao.getOneById(id);
        fileUtils.deleteFile(Constant.FILE_TEMPLATE_PATH, fileItemInfo.getFileTemplateFilename());
        fileItemInfoDao.deleteOneById(id);
        fiDepAssociationDao.deleteByItemId(id);
        return R.ok("deleteOne");
    }

    @Override
    public R getGrantList(Long itemId) {
        List<Long> grantedIds = fiDepAssociationDao.getGrantList(itemId);
        List<FileItemGrantDto> list = departmentDao.getFileItemGrantList();
        for (FileItemGrantDto fileItemGrantDto : list) {
            if (grantedIds.contains(fileItemGrantDto.getId())) {
                fileItemGrantDto.setIsGranted(1);
            } else {
                fileItemGrantDto.setIsGranted(0);
            }
        }
        Map<String, Object> data = new HashMap<>();
        data.put("list", list);
        data.put("total", list.size());
        return R.ok(data, "success");
    }

    @Override
    public R updateGrant(FileItemGrantVo fileItemGrantVo) {
        if (fileItemGrantVo.getItemId() == null) {
            return R.error("item's id is null");
        }
        if (fileItemGrantVo.getDepartmentIds().size() == 0) {
            fiDepAssociationDao.deleteByItemId(fileItemGrantVo.getItemId());
        } else {
            //fileItemInfoDao.updateGrant(fileItemGrantVo);
            List<Long> oldList = fiDepAssociationDao.getGrantListByItem(fileItemGrantVo.getItemId());
            List<Long> addList = new ArrayList<>(fileItemGrantVo.getDepartmentIds());
            addList.removeAll(oldList);
            List<Long> delList = new ArrayList<>(oldList);
            delList.removeAll(fileItemGrantVo.getDepartmentIds());
            if (!addList.isEmpty()) {
                fiDepAssociationDao.addByItemAndDepartment(fileItemGrantVo.getItemId(), addList);
            }
            if (!delList.isEmpty()) {
                fiDepAssociationDao.deleteByDepartmentId(fileItemGrantVo.getItemId(), delList);
            }
        }
        return R.ok("success");
    }

//    @Override
//    public R getItemList(Long departmentId) {
//        List<FileItemInfo> list = fileItemInfoDao.getItemList(departmentId);
//        Map<String, Object> data = new HashMap<>();
//        data.put("list", list);
//        return R.ok(data, "success");
//    }
    @Override
    public R getItemList(Long departmentId) {
        List<FileItemInfo> list = fileItemInfoDao.getItemList(departmentId);
        Map<String, Object> data = new HashMap<>();
        data.put("list", list);
        return R.ok(data, "success");
    }

    @Override
    public R uploadTemplate(MultipartFile file, Long fileItemId) {
        if (file == null) {
            return R.error("file is null,file is not found");
        }
        String filename;
        if (fileItemId == null) {
            filename = fileUtils.addFile(file, Constant.FILE_TEMPLATE_PATH);
        } else {
            FileItemInfo fileItemInfo = fileItemInfoDao.getOneById(fileItemId);
            filename = fileUtils.updateFiles(file, Constant.FILE_TEMPLATE_PATH, fileItemInfo.getFileTemplateFilename());
            fileItemInfoDao.updateFilenameAndUrlById(fileItemId, filename, Constant.GET_TEMPLATE_FILE_URL + filename);
            fileItemInfoDao.updateOneById(fileItemInfo);
        }
        if (filename == null) {
            return R.error("文件上传/更新失败");
        } else {
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("fileTemplateFilename", filename);
            data.put("fileTemplateUrl", Constant.GET_TEMPLATE_FILE_URL + filename);
            return R.ok(data, "upload successfully");
        }
    }

    @Override
    public R deleteTemplate(Long fileItemId) {
        FileItemInfo fileItemInfo = fileItemInfoDao.getOneById(fileItemId);
        fileUtils.deleteFile(Constant.FILE_TEMPLATE_PATH, fileItemInfo.getFileTemplateFilename());
        fileItemInfoDao.updateFilenameAndUrlById(fileItemId, "", "");
        return R.ok("delete successfully.");
    }
}
